home *** CD-ROM | disk | FTP | other *** search
- /****i* SOURCE_FILE/INFO
- *
- * NAME
- * Http.js
- *
- * USAGE
- * Part of Netobjects JavaScript Library.
- *
- * COPYRIGHT
- * Copyright ⌐ 2000-2005 Website Pros, Inc.
- * All Rights Reserved.
- *
- * This is an unpublished work protected by Website Pros, Inc.
- * as a trade secret, and is not to be used or disclosed except as
- * expressly provided in a written license agreement executed by
- * you and Website Pros, Inc.
- *
- * <copyright@websitepros.com>
- *
- * NOTES
- * JavaScript code.
- *
- *****/
- if (!IS_isModuleInitialized("IS.NOF.NET.Http"))
- {
-
- /****h* NOF_JavaScript_Library/NOF.NET.Http
- *
- * NAME
- * NOF.NET.Http
- *
- * DESCRIPTION
- *
- *
- ****/
-
- /**
- * Constructor
- * @param request - the request object;
- */
- function NET_Http( /*NET.HttpRequest*/ request ) {
- this.__proto__ = NET_Http.prototype;
-
- this.request = request;
- }
- {
- var member = NET_Http.prototype;
- member.CLASS_NAME = "NET.Http";
-
- var method = NET_Http.prototype;
- /**
- * Checks the online mode for the specified URL.
- * @param request (optional if the constructor was invoked with a HttpRequest object)
- * @return one of the following values:
- * -1: The argument does not specify an external address.
- * 0: The URL is online.
- * 1: The URL could not be connected.
- **/
- method.isOnline = function (/*NET.HttpRequest*/ request) {
- var METHOD_NAME = "isOnline";
- var _req = (request != null) ? request : this.request;
- if (_req != null) {
- var pUrl = (typeof(_req) == 'string') ? _req : _req.URL;
- return NOF.App.getFSIApp().OnlineMode(pUrl, false);
- }
- return -1;
- }
-
-
- /**
- * Performs a request on a given HttpRequest object.
- * @param request (optional if the constructor was invoked with a HttpRequest object)
- * @return a NOF.NET.HttpResponse object
- **/
- method.open = function (/*NOF.NET.HttpRequest*/ request) {
- var METHOD_NAME = "open";
- var _req = (request != null) ? request : this.request;
- var resp = null;
-
- if (_req != null) {
- // read request URL
- var pUrl = _req.URL;
- //TODO: validate this URL properly!
- if ( (pUrl == null) || (pUrl.indexOf("http") != 0) ) return null;
- var pHeader = "",
- pPostData = "";
- // read request headers
- var heads = _req.getHeaders();
- pHeader = this.convertHTTPHeaders(heads); //StringToHashMap
-
- var pIsGet = (_req.getMethod() == "GET");
- if (!pIsGet) {
- // read data to be posted only if it's a POST request
- pPostData = _req.getData();
- }
-
- var pIncludeHeader = _req.getIncludeHeaderResult();
- var pIncludeMessage = _req.getIncludeBodyResult();
-
- var respStr = NOF.App.getFSIApp2().HttpRequest2(pUrl, pHeader, pPostData, pIsGet, pIncludeHeader, pIncludeMessage);
-
- resp = new NOF.NET.HttpResponse();
- resp.URL = pUrl;
-
- if (respStr != null && respStr.length > 0) {
- if (!pIncludeHeader) {
- if (pIncludeMessage) {
- resp.content = respStr;
- }
- } else { // include headers
- if (!pIncludeMessage) { // only headers
- resp.headers = this.convertHTTPHeaders(respStr);
- } else {
- var parsedResponse = this.parseStream(respStr);
- resp.headers = this.convertHTTPHeaders(parsedResponse.headers);
- resp.content = parsedResponse.content;
- }
- resp.statusCode = resp.getHeader("status-code");
- }
- }
- }
- return resp;
- }
-
- /**
- * Download the content of a request to a specified (local) location.
- * @param localPath - the path (including the name) where the response file will be saved.
- * @param pUseProgress true if you want to show a progress bar during download.
- * @param request (optional if the constructor was invoked with a HttpRequest object)
- *
- * @return true if the download was succesfull.
- **/
- method.download = function ( /*String*/ localPath, /*boolean*/ pUseProgress, /*NOF.NET.HttpRequest*/ request) {
- var METHOD_NAME = "download";
- var _req = (request != null) ? request : this.request;
-
- //TODO: validate URL
- if ( (_req != null) && (_req.URL != null) && (_req.URL.indexOf("http") == 0) ) {
-
- var url = _req.URL;
- var postData = _req.getData();
-
- var iQMark = url.indexOf("?");
- if (iQMark > -1) {
- postData = (postData.length > 0) ? (postData + "&" + url.substring(iQMark + 1, url.length)) : (url.substring(iQMark + 1, url.length));
- url = url.substring(0, iQMark);
- _req.setMethod("POST");
- }
-
- var downloadResult = NOF.App.getFSIApp().HttpDownload(url, this.convertHTTPHeaders(_req.getHeaders()), postData, (_req.getMethod() == "GET"), localPath, pUseProgress);
- return downloadResult;
- }
-
- return false;
- }
-
- //private method
- method.convertHTTPHeaders = function ( headersObj ) {
- var retObj = null;
- if (headersObj != null) {
- if (typeof(headersObj.join) == "function") { //it is an array (or Hash)
- retObj = "";
- for (var h in headersObj) {
- //if (h == "status-code") continue; //exclude Status-Code?
- if (typeof(headersObj[h]) == "object" || typeof(headersObj[h]) == "string") {
- retObj += ("" + h + ": " + headersObj[h] + "\r\n");
- }
- }
- } else if ( (typeof(headersObj) == "string") || (typeof(headersObj.substring) == "function") ) {
- retObj = new Array();
- // get Status-Code
- //HTTP/1.1 200 OK or HTTP/1.1 404 Object Not Found
- if (headersObj.indexOf("HTTP/") == 0) {
- //get index of the end of the first line
- var iEOfL = headersObj.indexOf("\r\n");
-
- var iOfSpace = headersObj.indexOf(" ");
- var statusCode = headersObj.substring(iOfSpace + 1, headersObj.indexOf(" ", iOfSpace + 1));
- retObj["status-code"] = statusCode;
-
- headersObj = headersObj.substring(iEOfL + 2);
- }
- var hList = headersObj.split("\r\n");
- var hItem = null;
- var tmpList = null;
- for (var i = 0; i < hList.length; i++) {
- hItem = hList[i];
- if (hItem.length == 0) continue;
- tmpList = hItem.split(":");
- // all the headers name contains only lowercase letters
- retObj[tmpList[0].toLowerCase()] = tmpList[1].substring(1);
- }
- }
- }
- return retObj;
- }
-
- method.parseStream = function (str) {
- var obj = new Object();
- iOfFirstEmptyLine = str.indexOf("\r\n\r\n");
- obj.headers = str.substring(0, iOfFirstEmptyLine);
- obj.content = str.substring(iOfFirstEmptyLine + "\r\n\r\n".length);
- return obj;
- }
- }
-
- NOF.NET.__proto__.Http = NET_Http;
- }